import { BasicMarker } from '../../js/BasicMarker.js';
import { BasicMarkers } from '../../js/BasicMarkers.js';
import { getMarkerIcon } from '../../js/icon.js';
// import { openGoodsDeviceDialog } from '@/views/equipment-goods-manage/components/overview-info-dialog';
import { useUserInfoStore } from '@gis/lib/pinia/userInfo.js';
import { handleCommonResult } from '@gis/lib/utils/resolver.js';
import { getDevLocation, wlsbPoints } from '@gis/lib/api';
import get from 'lodash-es/get.js';
import { openEquipmentRlmjDetailsDialog, openGoodsDeviceDialog } from '../../../../views/equipment/opt.js';
import { openMonitorDetailsDialog } from '../../../../views/monitor/monitor-opt.js';
import { openEquipmentCldzDetailsDialog, openEquipmentDjzdDetailsDialog } from '@gis/modules/views/equipment/opt.js';

const typeCodeEnum = {
  rlmj: 'rlmj', //人脸门禁
  cldz: 'cldz', //车辆道闸
  djzd: 'djzd', //登记终端
  xlgc: 'xlgc', //监控设备
  D0114_1: 'yg', // 烟感
  D0112_6: 'gkpw', // 高空抛物
};

class Marker extends BasicMarker {
  constructor({ map, data }) {
    super({ map, data });
    /* this.type: 这个type主要用于筛选,需要和filter-legend里面的type相对应 */
    this.type = typeCodeEnum[data.type];
    super.create();
  }
  getIcon() {
    return getMarkerIcon(this.type);
  }
  onClick() {
    if (this.data.type == 'rlmj') {
      openEquipmentRlmjDetailsDialog(this.data);
    } else if (this.data.type == 'cldz') {
      openEquipmentCldzDetailsDialog(this.data);
    } else if (this.data.type == 'djzd') {
      openEquipmentDjzdDetailsDialog(this.data);
    } else if (this.data.type == 'D0114_1') {
      openGoodsDeviceDialog(this.data);
    } else {
      openMonitorDetailsDialog(this.data);
    }
  }
}

export default class Bhaqffwlsb extends BasicMarkers {
  constructor({ map }) {
    super();
    this.map = map;
  }
  createMarker(data) {
    return new Marker({ map: this.map, data });
  }
  async getDataAsync() {
    const { area_code } = useUserInfoStore();
    const params = {
      area_code: area_code,
    };

    const res = await getDevLocation(params);
    const data = res ? res.data || {} : {};
    /* 监控 */
    // 高空抛物 - 取的高空摄像
    const gkpwList = get(data, 'xlgc_type.D0112_6', []);
    /* xlgc_type 这个字段返回了雪亮工程中所有类型，跟 xlgc 字段中的数据一致，只是格式不一样，目前暂时用不着 */
    delete data.xlgc_type;
    let monitorList = ['rlmj', 'cldz', 'djzd', 'xlgc'].reduce((res, key) => {
      const list = get(data, key, []);
      return [].concat(
        res,
        list
          .filter((item) => item.latitude != '0' && item.longitude != '0')
          .map((item) => {
            return Object.assign(item, {
              type: key,
              lnglat: [item.longitude, item.latitude],
            });
          })
      );
    }, gkpwList);
    /* 设备:烟感 */
    let wlsbList = handleCommonResult(await wlsbPoints(params));
    wlsbList = wlsbList
      .filter((item) => item.type == 'D0114_1')
      .map((item) => Object.assign(item, { lnglat: [item.longitude, item.latitude] }));
    return [].concat(monitorList, wlsbList);
  }
}
